import QtQuick 1.0
import "../WebAccess/HttpAccess.js" as HttpAccess
import "../Util/Util.js" as Util
import "../Util/IncSearch.js" as IncSearch

ListModel {
    id: _root

    default property alias roles: _xmlListModel.roles
    property alias source: _xmlListModel.source
    property alias query: _xmlListModel.query
    property alias namespaceDeclarations: _xmlListModel.namespaceDeclarations
    property alias xml: _xmlListModel.xml
    property alias status: _xmlListModel.status
    property bool isAppendTop: false                  //上に追加するか

    signal finishAppend()


    function push(item){
        append(item);
    }


    //解析をさせるXmlListModel
    property variant __xmlListModel: XmlListModel {
        id: _xmlListModel

        property variant keys: []

        onSourceChanged: {
//            console.debug("XmlListModel:onSourceChanged:\n" + source)
        }
        onXmlChanged: {
//            console.debug("XmlListModel:onXmlChanged:\n" + xml)
        }
        onStatusChanged: {
            if(status == XmlListModel.Ready){
//                console.debug("XmlListModel:onStatusChanged: Ready");
                isAppendTop = false;
            }else{
//                console.debug("XmlListModel:onStatusChanged: Busy");
            }
        }

        //追加
        function appendItem(index, count){
//            Util.outputDebugString("_xmlListModel.appendItem:" + index + "," + count);

            var insert_count = 0;

            var i = 0, j = 0, k = 0;
            for (i = 0; i < count; i++) {
                var item = get(i + index);
                var found = false;

                //キーが長さ零ならマッチしたことにして登録しない
                for (k = 0; k < _xmlListModel.keys.length; k++) {
                    if (item[_xmlListModel.keys[k]].length === 0){
                        found = true;
                        break;
                    }
                }
                //重複確認
                for (j = 0; j < _root.count; j++) {
                    var item2 = _root.get(j);
                    var match = true;
                    for (k = 0; k < _xmlListModel.keys.length; k++) {
                        if (item[_xmlListModel.keys[k]] != item2[_xmlListModel.keys[k]]) {
                            match = false;
                            break;
                        }
                    }
                    if (match) {
                        found = true;
                        break;
                    }
                }

                if (!found) {
//                    Util.outputDebugString("_xmlListModel.appendItem:found:" + item);
                    if(!isAppendTop){
                        //後ろへ追加
                        push(item);
                    }else{
                        insert(insert_count, item);
                        insert_count++;
                    }
                }

                // インクリメンタルサーチ用の登録
                IncSearch.add(IncSearch.SEARCH_KIND_SCREEN_NAME, item["_screen_name"]);
                if(item["_hashtags"] === undefined){
                }else if(item["_hashtags"].length === 0){
                }else{
                    var tags = Util.pickUpHashTagsFromEntities(item["_hashtags"]);
                    for(var tag_i=0; tag_i<tags.length; tag_i++){
                        IncSearch.add(IncSearch.SEARCH_KIND_HASH_TAG, tags[tag_i]);
                    }
                }
            }

            //必ず落とす
            isAppendTop = false;

            //追加完了イベント
            finishAppend();
        }

        onItemsInserted: appendItem(index, count)

    }

    Component.onCompleted: {
        //Util.outputDebugString("ListModel.onCompleted");

        var keys = new Array();
        for(var i = 0; i<roles.length; i++){
            if(roles[i].isKey){
                keys.push(roles[i].name);
            }
        }
        _xmlListModel.keys = keys;

    }
}
